package com.example.restaurant.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.restaurant.common.R;
import com.example.restaurant.dto.UserDto;
import com.example.restaurant.entity.User;
import com.example.restaurant.service.UserService;
import com.example.restaurant.utils.JwtUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import com.example.restaurant.utils.JwtUtil;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @BelongsProject:
 * @BelongsPackage: com.example.restaurant.controller
 * @Author: chenAng
 * @CreateTime: 2023-12-12  22:50
 * @Version: 1.0
 */
@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/api")
public class UserController {

    @Autowired
    private JwtUtil jwtUtil;
    @Resource
    private UserService userService;

    //登录
    @PostMapping("/user/login")
    public R<String> login(HttpServletRequest request, @RequestBody User user,HttpSession session){
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(User::getPhone,user.getPhone());
        User user1 = userService.getOne(userLambdaQueryWrapper);
        if (user1==null){
            return R.error("用户未注册");
        }
        userLambdaQueryWrapper.eq(User::getPassword,user.getPassword());
        User user2 = userService.getOne(userLambdaQueryWrapper);
        if(user2!=null){
            if (user2.getStatus()==1){
                String jwt = jwtUtil.createJWT(user2);
                return R.success(jwt);
            }else {
                return R.error("用户已禁用");
            }
        }else {
            return R.error("用户名或密码不正确");
        }

    }




    @PostMapping("/user/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("user");
        return R.success("退出成功");
    }


    @RequestMapping ("/user/getUserIdFromToken")
    public R<UserDto> getUserIdFromToken(HttpServletRequest request){

        String token = request.getHeader("Sessionid");
        User user = jwtUtil.getToken(token);
//        System.out.println(user);
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(User::getPhone,user.getPhone());
        User user1 = userService.getOne(userLambdaQueryWrapper);
        UserDto userDto = new UserDto();
        userDto.setUserid(user1.getUserid());
        userDto.setUname(user1.getUname());
        userDto.setAge(user1.getAge());
        userDto.setSex(user1.getSex());
        userDto.setStatus(user1.getStatus());
        userDto.setAvatar(user1.getAvatar());
        userDto.setIntroduction(user1.getIntroduction());
        userDto.setBirthday(user1.getBirthday());
        userDto.setPassword(user1.getPassword());
        userDto.setPhone(user1.getPhone());
        userDto.setPoints(user1.getPoints());
        userDto.setSignature(user1.getSignature());
        userDto.setMyFans(33);
        userDto.setMyFocus(55);
        return R.success(userDto);
    }

    //id  用户id
    @RequestMapping ("/user/queryUserMsg")
    public R<User> queryUserMsg(@RequestParam("id") String id){
//        String token = request.getHeader("Sessionid");
//        User user = jwtUtil.getToken(token);

//        LambdaQueryWrapper<Chat> chatLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        chatLambdaQueryWrapper.eq(Chat::getSendid, user.getUserid())
//                .eq(Chat::getReceiverid, id)
//                .or()
//                .eq(Chat::getSendid, id)
//                .eq(Chat::getReceiverid, user.getUserid())
//                .orderByAsc(Chat::getSendtime);
//        List<Chat> list = chatService.list(chatLambdaQueryWrapper);


        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(User::getUserid,id);

        User personinfo = userService.getOne(queryWrapper);

        return R.success(personinfo);
    }

//    register
    @RequestMapping ("/user/sendRegisterEmail")
    public R<String> sendRegisterEmail(@Param("phone") String phone){
        String verificationCode = generateVerificationCode(4);
        System.out.println(verificationCode);
        return R.success(verificationCode);
    }

    @RequestMapping ("/user/register")
    public R<String> register(@RequestBody User user){

        return R.success("");
    }

    //随机生成验证码
    public static String generateVerificationCode(int length) {
        Random random = new Random();
        StringBuilder code = new StringBuilder();

        for (int i = 0; i < length; i++) {
            int digit = random.nextInt(10); // 生成0-9之间的随机数
            code.append(digit);
        }
        return code.toString();
    }


}
